package com.thuanpq60023.easyenglish.view;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class EditextState {
	private Activity mActivity;

	public EditextState(Activity activity) {
		this.mActivity = activity;
	}

	public void setListener(final EditText ediText) {
		ediText.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				ediText.setFocusable(true);
				ediText.requestFocus(EditText.FOCUS_DOWN);
				ediText.requestFocusFromTouch();
			}
		});

		ediText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				InputMethodManager imm = (InputMethodManager) mActivity
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				if (hasFocus == true) {
					if (imm != null) {
						imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
								InputMethodManager.HIDE_IMPLICIT_ONLY);
					}
				} else {
					if (imm != null) {
						imm.hideSoftInputFromWindow(ediText.getWindowToken(), 0);
					}
				}
			}
		});
	}
}
